//
//  GFForgotPwdViewController.m
//  Golf
//
//  Created by 王竞成 on 16/5/6.
//  Copyright © 2016年 刘俊. All rights reserved.
//

#import "GFForgotPwdViewController.h"
#import "ViewTemplate.h"
#import "MY_infoModel.h"
//#import "WKAlertView.h"

@interface GFForgotPwdViewController ()<UITextFieldDelegate>
{
    NSTimer *timer;
    NSInteger index;
    UITextField *phonefid;//手机号码
    NSString *firstS;
    NSString *secStr;
}
@property (nonatomic,strong)UIButton *btnGetCode;                       //获取验证码
@property (nonatomic, strong)NSTimer *timerCheck;                       //获取验证码定时器

@end
#define kGetCodeType 0x0002
#define kGetBakPassward 0x0003

@implementation GFForgotPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    index = 60;
    self.title = @"忘记密码";
    self.view.backgroundColor = HexRGB(0xf4f4f4);
    [self initView];

    // Do any additional setup after loading the view from its nib.
}
#pragma mark -- http Request
//获取验证码
- (void)getVerificationCodeAfn{
//    if (!self.isLoading) {
//        NSMutableDictionary *params = @{}.mutableCopy;
//        [params setValue:phonefid.text forKey:@"phoneNum"];
//        [params setValue:@"2" forKey:@"useType"];
//        self.requestType = kGetCodeType;
//        [self.requestClient requestPost:kGetVerificationCode withParameters:params isEncryp:NO];
//    }
    [self analyticalData:nil];
}
-(void)analyticalData:(NSDictionary *)data{
//    switch (self.requestType) {
//        case kGetCodeType:
            [self showHUDWithText:@"验证码发送成功" AfterDelay:1];
            index = 60;
            timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reloadtime) userInfo:nil repeats:YES];
//            break;
//            case kGetBakPassward:
//               [self initpwd:data];
//            break;
//        default:
//            break;
//    }
}
#pragma #pragma mark - UI加载
- (void)initView{
    UIView *firstview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    firstview.tag = 2055;
    MY_infoModel *model;
    //手机号
    model = [[MY_infoModel alloc]init];
    model.title = @"手机";
    model.prompt = @"请输入你的手机号";
    model.isclear = YES;
    model.Tag = 2050;
    model.btntitle = @"";
    ViewTemplate *phone = [[ViewTemplate alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 50) Model:model];
    [firstview addSubview:phone];
    
    //验证码
    model = [[MY_infoModel alloc]init];
    model.title = @"验证码";
    model.prompt = @"请输入验证码";
    model.isclear = NO;
    model.Tag = 2051;
    model.btntitle = @"获取验证码";
    ViewTemplate *validation = [[ViewTemplate alloc]initWithFrame:CGRectMake(0, phone.golf_height + phone.golf_top + 1, kScreenWidth, 50) Model:model];
    validation.tag = 3001;
    validation.promptbtn.backgroundColor = HexRGB(0xff6816);
    validation.promptbtn.selected = YES;
    validation.promptbtn.tag = 3000;
    [validation.promptbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [firstview addSubview:validation];
    //下一步
    UIButton *nextbtn = [[UIButton alloc]initWithFrame:CGRectMake(15, validation.golf_top + validation.golf_height + 20, kScreenWidth - 30, 44)];
    nextbtn.tag = 2052;
    nextbtn.backgroundColor = [UIColor redColor];
    [nextbtn setTitle:@"下一步" forState:0];
    nextbtn.backgroundColor = HexRGB(0xd8d8d8);
    nextbtn.selected = NO;
    nextbtn.layer.cornerRadius = CORNERRADIUS_BTN;
    [nextbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [firstview addSubview:nextbtn];
    
    [self.view addSubview:firstview];
    
    phonefid = (UITextField *)[self.view viewWithTag:2050];
    phonefid.delegate = self;
    [phonefid addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    UITextField *validationfid = (UITextField *)[self.view viewWithTag:2051];
    validationfid.delegate = self;
    [validationfid addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}

//下一步
- (void)nextview{
    UIView *nextView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    MY_infoModel *model1;
    //设置新密码
    model1 = [[MY_infoModel alloc]init];
    model1.title = @"新密码";
    model1.prompt = @"请输入你的密码";
    model1.isclear = NO;
    model1.Tag = 2053;
    model1.btntitle = @"显示";
    
    MY_infoModel *model2;
    model2 = [[MY_infoModel alloc]init];
    model2.title = @"再次输入";
    model2.prompt = @"请再次输入你的密码";
    model2.isclear = NO;
    model2.Tag = 9999;
    model1.btntitle = @"显示";

    NSArray *moderArr = @[model1,model2];
    for (int i = 0; i<2; i++) {
        ViewTemplate * pwd = [[ViewTemplate alloc]initWithFrame:CGRectMake(0, 20 + i*70, nextView.golf_width, 50) Model:moderArr[i]];
        pwd.promptbtn.tag = 2056 +i;
        pwd.promptbtn.selected = NO;
        pwd.promptbtn.backgroundColor = [UIColor clearColor];
        pwd.promptbtn.golf_left = kScreenWidth - pwd.promptbtn.golf_width;
//        [pwd.promptbtn setTitle:@"" forState:0];
//        [pwd.promptbtn setImage:[UIImage imageNamed:@"forget password_icn_eye_show"] forState:UIControlStateSelected];
//        [pwd.promptbtn setImage:[UIImage imageNamed:@"forget password_icn_eye_hide"] forState:0];
//        [pwd.promptbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [nextView addSubview:pwd];

    }
       //完成

    UIButton *nextbtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 160, kScreenWidth - 30, 44)];
    nextbtn.tag = 2054;
    [nextbtn setTitle:@"完成" forState:0];
    nextbtn.backgroundColor = HexRGB(0xd8d8d8);
    nextbtn.titleLabel.font = Font(16);
    nextbtn.layer.cornerRadius = CORNERRADIUS_BTN;
    [nextbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [nextView addSubview:nextbtn];
    [self.view addSubview:nextView];
    
    UITextField *pwdfid = (UITextField *)[self.view viewWithTag:2053];
    UITextField *pwdse = (UITextField *)[self.view viewWithTag:9999];

    pwdfid.secureTextEntry = YES;
    pwdfid.delegate = self;
    [pwdfid addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    pwdse.secureTextEntry = YES;
    pwdse.delegate = self;
    [pwdse addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


}



#pragma #pragma mark - textfield的代理
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

//倒计时
- (void)reloadtime{
    index --;
    ViewTemplate *view = (ViewTemplate *)[self.view viewWithTag:3001];
    
    if (index == -1) {
        [view.promptbtn setTitle:@"重新获取" forState:0];
        view.promptbtn.selected = YES;
        view.promptbtn.backgroundColor = HexRGB(0xff6816);
        [timer invalidate];
    }else{
        view.promptbtn.selected = NO;
        [view.promptbtn setTitle:[NSString stringWithFormat:@"剩余%ldS",(long)index] forState:0];
        view.promptbtn.backgroundColor = HexRGB(0xd8d8d8);
    }
    
}

#pragma #pragma mark - 点击响应事件
//btn
- (void)btnClick:(UIButton *)sender{
    if (sender.tag == 3000) {
        //验证码button
        if (sender.selected) {
            if (sender.selected) {
                UITextField *textfid = (UITextField *)[self.view viewWithTag:2050];
                NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
                BOOL isMatch = [pred evaluateWithObject:textfid.text];
                if (isMatch) {
                    [self getVerificationCodeAfn];
                }else{
                    [self showHUDWithText:@"手机号码格式不对" AfterDelay:1];
                }
                
            }
            
        }
    }
    if (sender.tag == 2052) {
        if (sender.selected) {
            UIView *view = [self.view viewWithTag:2055];
            view.hidden = YES;
            [self nextview];
        }
        
        
    }
    if (sender.tag == 2054) {
        if ([firstS isEqualToString:secStr]) {
            if (sender.selected) {
                //手机号，验证码，新密码，发请求
                UITextField *pwdfid = (UITextField*)[self.view viewWithTag:2053];
                UITextField *phone = (UITextField*)[self.view viewWithTag:2050];
                UITextField *val = (UITextField*)[self.view viewWithTag:2051];
                //字母开头的6-12位数，正则表达式
                NSString *regex = @"^[a-zA-z][0-9a-zA-Z]{5,11}$";
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
                BOOL isMatch = [pred evaluateWithObject:pwdfid.text];
                if (isMatch) {
                    //密码找回请求
                    
                }else{
                    [self showHUDWithText:@"请输入以字母开头的6-12位数字和字母组合" AfterDelay:1];
                }
                
            }

        }else{
            
            [self showHUDWithInfo:@"输入错误，请重新输入" AfterDelay:1];
        }
}
    if (sender.tag == 2056) {
        UITextField *pwdfid = (UITextField *)[self.view viewWithTag:2053];
        if (sender.selected) {
            sender.selected = NO;
            pwdfid.secureTextEntry = YES;
        }else{
            sender.selected = YES;
            pwdfid.secureTextEntry = NO;
        }
    }
}

- (void)initpwd:(NSDictionary *)dic{
        [self showHUDWithText:@"修改成功" AfterDelay:1];
        [self.navigationController popViewControllerAnimated:YES];
}

//文本
- (void) textFieldDidChange:(UITextField *) textField {
    if (textField.tag == 2050 || textField.tag == 2051) {
        UITextField *phonef = (UITextField *)[self.view viewWithTag:2050];
        UITextField *validationfid = (UITextField *)[self.view viewWithTag:2051];
        UIButton *nextbtn = (UIButton *)[self.view viewWithTag:2052];
        if ([phonef.text isEqualToString:@""] || [validationfid.text isEqualToString:@""]) {
            nextbtn.selected = NO;
            nextbtn.backgroundColor = HexRGB(0xd8d8d8);
        }else{
            nextbtn.selected = YES;
            nextbtn.backgroundColor = HexRGB(0xff6816);
        }
    }
    if (textField.tag == 2053 || textField.tag == 9999) {
        //提交后重新设置时候的textfield
        UITextField *pwdfirst = (UITextField *)[self.view viewWithTag:2053];
        UITextField *pwdSec = (UITextField *)[self.view viewWithTag:9999];
        firstS = pwdfirst.text;
        secStr = pwdSec.text;
        UIButton *nextbtn = (UIButton *)[self.view viewWithTag:2054];
        if ([firstS isEqualToString:@""] || [secStr isEqualToString:@""]) {
            nextbtn.selected = NO;//都为空按钮不能选择
            nextbtn.backgroundColor = HexRGB(0xd8d8d8);
        }else{
            nextbtn.selected = YES;
            nextbtn.backgroundColor = HexRGB(0xff6816);
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
